package com.library.cx.setvlet;



import com.library.cx.dao.Libraryimpl;
import com.library.cx.dao.Librarys;
import com.library.cx.into.Libary;
import com.library.cx.service.Totalimpl;
import com.library.cx.util.MybatisUtil;
import com.library.cx.util.Page;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LibaryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SqlSession sqlSession = MybatisUtil.getSqlSession();
       req.setCharacterEncoding("UTF-8");
        Totalimpl totalimpl = new Totalimpl();
        String names=req.getParameter("name");//用户名
        if(names==null){
            names="璇哥";
        }
        String password=req.getParameter("password");//密码
        String denglu=req.getParameter("denglu");//获取登录的按钮

            Libraryimpl libraryimpl = new Libraryimpl();
            //判断是否点击登录的按钮
            if (denglu!=null) {
                int denglu1 = sqlSession.getMapper(Librarys.class).denglu(names, password);
                int counts = Integer.parseInt(String.valueOf(denglu1));
                if (counts == 1) {
                    //判断是否成功
                    req.setAttribute("deng",names);
                    req.setAttribute("password",password);
                    req.getRequestDispatcher("cx/zhuye.jsp").forward(req, resp);
                }else {
                    req.getRequestDispatcher("cx/logess.jsp").forward(req,resp);
                }
            }


        /**
        * 添加的验证
         */
        String insert=req.getParameter("tp");
        //判断是否点击提交按钮
        if (insert!=null){
            /**
             * 获取数值
             */
            String bookName=req.getParameter("bookName");
           Integer type=Integer.parseInt(req.getParameter("type"));
           String authors=req.getParameter("author");
           Integer sum=Integer.parseInt(req.getParameter("sum"));
            System.out.println(sum);
           Integer price=Integer.parseInt(req.getParameter("price"));
           String location=totalimpl.getRandomString();
           String source=req.getParameter("source");
           String jdate=req.getParameter("jdate");
            Map map=new HashMap();
            map.put("bookName",bookName);
            map.put("type",type);
            map.put("authors",authors);
            map.put("sum",sum);
            map.put("price",price);
            map.put("location",location);
            map.put("source",source);
            map.put("jdate",jdate);
            int count = sqlSession.getMapper(Librarys.class).insert(map);
            //判断是否成功
           if (count==1){

                req.getRequestDispatcher("cx/Inserts.jsp").forward(req,resp);
           }
         }
        /**
         * 修改的验证
         */
        String update= req.getParameter("up");
        //是否点击修改
        if (update!=null){
            Integer ids= Integer.parseInt(req.getParameter("id"));
            String bookNames=req.getParameter("bookName");
            String types=req.getParameter("typeName");
            String authors=req.getParameter("author");
            Integer sums=Integer.parseInt(req.getParameter("sum"));
            Integer prices=Integer.parseInt(req.getParameter("price"));
            String locations=req.getParameter("location");
            String sources=req.getParameter("source");
            String jdates=req.getParameter("jdate");
            List fin=new ArrayList();
            fin.add(ids);
            fin.add(bookNames);
            fin.add(types);
            fin.add(authors);
            fin.add(sums);
            fin.add(prices);
            fin.add(locations);
            fin.add(sources);
            fin.add(jdates);
            req.setAttribute("fin",fin);
            req.getRequestDispatcher("cx/Update.jsp").forward(req,resp);
        }
        String Update=req.getParameter("tps");
        /*
        修改书籍信息
         */
        if (Update!=null){
            Integer ids= Integer.parseInt(req.getParameter("bookId"));
            String bookNames=req.getParameter("bookNames");
            String types=req.getParameter("types");
            String authors=req.getParameter("authors");
            Integer sums=Integer.parseInt(req.getParameter("sums"));
            Integer prices=Integer.parseInt(req.getParameter("prices"));
            String locations=Totalimpl.getRandomString();
            String sources=req.getParameter("sources");
            String jdates=req.getParameter("jdates");
            if (types=="网络小说"||types.equals("网络小说")){
                types="1";
            }
            if (types=="儿童益智"||types.equals("儿童益智")){
                types="2";
            }
            if (types=="学习资料"||types.equals("学习资料")){
                types="3";
            }
            if (types=="经典名著"||types.equals("经典名著")){
                types="4";
            }
            Integer type=Integer.parseInt(types);
            Map map=new HashMap();
            map.put("ids",ids);
            map.put("bookNames",bookNames);
            map.put("type",type);
            map.put("authors",authors);
            map.put("sums",sums);
            map.put("prices",prices);
            map.put("locations",locations);
            map.put("sources",sources);
            map.put("jdates",jdates);
            int count = sqlSession.getMapper(Librarys.class).update(map);
            if (count==1){
                req.setAttribute("deng",names);
                req.getRequestDispatcher("cx/Administrator.jsp").forward(req,resp);
            }
        }
        /*
        主页点击修改密码
         */
        String Upmima=req.getParameter("Uppad");
        if (Upmima!=null){
            String deng=req.getParameter("deng");
            req.setAttribute("deng",deng);
                req.getRequestDispatcher("cx/MimaUpdate.jsp").forward(req,resp);
        }
        /*
        修改密码页面
         */
        String com=req.getParameter("tpssss");
        if (com!=null){
            String name=req.getParameter("name");
            String pad=req.getParameter("pad");
            Map map=new HashMap();
            map.put("pwd",pad);
            map.put("name",name);
            int count = sqlSession.getMapper(Librarys.class).updateadmit(map);
            if (count==1){
                req.getRequestDispatcher("cx/logess.jsp").forward(req,resp);
            }
        }
        /*
        忘记管理员密码
         */
        String czm=req.getParameter("updatewang");
        if(czm!=null){
            String name=req.getParameter("name");
            String phone=req.getParameter("phone");
            String pwd=req.getParameter("password");
            Map map=new HashMap();
            map.put("name",name);
            map.put("phone",phone);
            map.put("pwd",pwd);
            int count = sqlSession.getMapper(Librarys.class).admin(map);
            System.out.println(count);
            if (count==1){
                System.out.println("忘记密码");
                req.getRequestDispatcher("cx/logess.jsp").forward(req,resp);
            }
        }
        String bookname=req.getParameter("bookname");
        String author=req.getParameter("author");
        String currentPage = req.getParameter("currentPage");
        //页数
        if (currentPage == null) {
            currentPage = "1";
        }
        //未输入书名就默认为全部
        if(bookname==null||bookname.equals("")){
            bookname="全部";
        }
        //如果作者未输入,就默认为空
        if (author==null||author.equals("")){
            author="全部";
        }
        int countss=Integer.parseInt(currentPage);
        /**
         * 删除的验证
         */
        String id=req.getParameter("id");
        if (id==null){
            id="0";
        }
        Integer bookId=Integer.parseInt(id);
        String delete=req.getParameter("delete");
        if(delete!=null){
            int deletesum = sqlSession.getMapper(Librarys.class).detece(bookId);
            //判断是否成功
            if (deletesum==1){
                Page<Libary> finBay=totalimpl.findByPage(bookname,author,countss);
                req.setAttribute("bays",finBay);
                req.setAttribute("deng",names);
                req.getRequestDispatcher("cx/Administrator.jsp").forward(req,resp);
            }
        }

        /**
         * 查询的验证
         */

        String select =req.getParameter("submit");

        if (select==null) {
            select = "初始值";
        }
        //判断是否点击查询按钮
        if (select!=null){
            //判断是否成功
            //String deng =req.getParameter("denglu");
            Page<Libary> finBay=totalimpl.findByPage(bookname,author,countss);
            req.setAttribute("bays",finBay);
            req.setAttribute("deng",names);
            req.getRequestDispatcher("cx/Administrator.jsp").forward(req,resp);
        }
        sqlSession.commit();
        sqlSession.close();
    }
}
